package com.xhlj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xhlj.entity.Agenda;
import com.xhlj.entity.Conference;
import com.xhlj.entity.Speaker;
import com.xhlj.mapper.ConferenceMapper;
import com.xhlj.service.AgendaService;
import com.xhlj.service.ConferenceService;
import com.xhlj.service.SpeakerService;
import com.xhlj.utils.Result;
import jakarta.annotation.Resource;
import jakarta.faces.application.ResourceDependencies;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;



/**
 * @author dell
 * @description 针对表【conference】的数据库操作Service实现
 * @createDate 2024-03-01 10:38:12
 */
@Service
public class ConferenceServiceImpl extends ServiceImpl<ConferenceMapper, Conference>
        implements ConferenceService {
    @Resource
    private ConferenceMapper conferenceMapper;
    @Resource
    private SpeakerService speakerService;

    @Resource
    private AgendaService agendaService;
    @Override
    public Result getConferenceList() {
        List<Conference> conferenceList = conferenceMapper.getConferenceList();
        /*转换speakerId为speakerList*/
        conferenceList.stream().forEach(item -> {
            List<Agenda> agendaList = item.getAgendaList();
            agendaList.stream().forEach(agenda -> {
                String speakerId = agenda.getSpeakerId();
                String[] speakers = speakerId.split(",");
//                List<String> collect = Arrays.stream(speakers).map(speaker -> {
//                    return speaker.trim();
//                }).collect(Collectors.toList());
                LambdaQueryWrapper<Speaker> wrapper = new LambdaQueryWrapper<>();
                for (String id : speakers) {
                    wrapper.eq(Speaker::getAttendeeId, Integer.parseInt(id)).or();
                }
                List<Speaker> list = speakerService.list(wrapper);
                agenda.setSpeakerList(list);
            });
        });


        return Result.ok(conferenceList);
    }

    @Override
    public Result getConferenceMsgByDate(Date date) {
        //TODO:根据日期获取会议表
        LambdaQueryWrapper<Conference> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Conference::getConferenceDate,date);
        List<Conference> conferenceList = conferenceMapper.selectList(queryWrapper);
        for(Conference con:conferenceList){
            //TODO:以会议id为条件去议程中查找相关联的议程
            Integer conferenceId = con.getConferenceId();
            LambdaQueryWrapper<Agenda> queryWrapper_a=new LambdaQueryWrapper<>();
            queryWrapper_a.eq(Agenda::getConferenceId,conferenceId);
            List<Agenda> agendaList = agendaService.list(queryWrapper_a);

            for(Agenda agenda:agendaList){
                //TODO:以议程id为条件去查找相关联的演讲者
                //TODO:演讲者id可能为多个并以“，”分割，将其转化为列表
                String speakerId = agenda.getSpeakerId();
                String[] speakers = speakerId.split(",");
                LambdaQueryWrapper<Speaker> queryWrapper_s=new LambdaQueryWrapper<>();
                for(String id:speakers){
                    queryWrapper_s.eq(Speaker::getAttendeeId, Integer.parseInt(id)).or();
                }
                List<Speaker> list = speakerService.list(queryWrapper_s);
                agenda.setSpeakerList(list);
            }
            con.setAgendaList(agendaList);
        }
        return Result.ok(conferenceList);
    }
}




